﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{
    class class_ISYC_304_07
    {
        public static void ISYC_304_F42(Dictionary<string, Object> data)  //凹半圆铣刀
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //作图
            //参数
            double pi = Math.PI;
            double soal = Convert.ToDouble(data["oal"]);
            double sdc = Convert.ToDouble(data["dc"]);
            double scale = Convert.ToDouble(data["scale"]); //放大系数
            if (scale == 0)
            {
                if (sdc < 39 && soal < 90)
                {
                    scale = 4.0;
                }
                else if (sdc < 52 && soal < 120)
                {
                    scale = 3.0;
                }
                else if (sdc < 78 && soal < 180)
                {
                    scale = 2.0;
                }
                else if (sdc < 104 && soal < 240)
                {
                    scale = 1.5;
                }
                else if (sdc < 156 && soal < 360)
                {
                    scale = 1.0;
                }
                else if (sdc < 208 && soal < 480)
                {
                    scale = 0.75;
                }
                else
                {
                    scale = 0.5;
                }
            }
            double dc = Convert.ToDouble(data["dc"]) * scale;
            double dcon = Convert.ToDouble(data["dconms"]) * scale;
            double oal = Convert.ToDouble(data["oal"]) * scale;
            double dhub = Convert.ToDouble(data["dhub"]) * scale;
            double cw = Convert.ToDouble(data["cw"]) * scale;
            double hhub = Convert.ToDouble(data["hhub"]) * scale;
            double bcdpax = Convert.ToDouble(data["bcdpax"]) * scale;
            if (bcdpax == 0)
            {
                bcdpax = 2 * scale;
            }
            double prfrad = Convert.ToDouble(data["prfrad"]) * scale;
            double cdx = (dc - dhub) / 2;
            double isframe = Convert.ToDouble(data["isframe"]);
            string path = Convert.ToString(data["path"]);
            //坐标
            //主视图
            Vector2 v1 = new Vector2(0, dhub / 2);
            Vector2 v2 = new Vector2(-hhub, v1.Y);
            Vector2 v3 = new Vector2(v2.X, dc / 2);
            Vector2 v4 = new Vector2(-oal / 2 + prfrad, v3.Y);
            Vector2 v5 = new Vector2(-oal / 2 - prfrad, v3.Y);
            Vector2 v6 = new Vector2(-oal + hhub, v5.Y);
            Vector2 v7 = new Vector2(v6.X, v2.Y);
            Vector2 v8 = new Vector2(-oal, v7.Y);
            Vector2 v9 = new Vector2(v8.X, -v8.Y);
            Vector2 v10 = new Vector2(v7.X, -v7.Y);
            Vector2 v11 = new Vector2(v6.X, -v6.Y);
            Vector2 v12 = new Vector2(v5.X, -v5.Y);
            Vector2 v13 = new Vector2(v4.X, -v4.Y);
            Vector2 v14 = new Vector2(v3.X, -v3.Y);
            Vector2 v15 = new Vector2(v2.X, -v2.Y);
            Vector2 v16 = new Vector2(v1.X, -v1.Y);
            Vector2 v17 = new Vector2(v4.X + bcdpax, v4.Y);
            Vector2 v18 = new Vector2(v5.X - bcdpax, v5.Y);
            Vector2 v19 = new Vector2(v18.X, -v18.Y);
            Vector2 v20 = new Vector2(v17.X, -v17.Y);
            Vector2 v21 = new Vector2(0, -dcon / 2);
            Vector2 v22 = new Vector2(-oal, v21.Y);
            Vector2 v23 = new Vector2(v22.X, -v22.Y);
            Vector2 v24 = new Vector2(v21.X, -v21.Y);
            Vector2 v25 = new Vector2(-oal - 2, 0);
            Vector2 v26 = new Vector2(2, 0);
            Vector2 add1 = fun.Inters(v4, fun.Polar(v4, -90 * pi / 180, dc), v25, v26);
            Vector2 add2 = fun.Inters(v5, fun.Polar(v5, -90 * pi / 180, dc), v25, v26);
            //线段
            //主视图
            Vector2[] Line1 = { v1, v2 };
            fun.drawBLine(dxf, Line1, 0.3, contour);
            Vector2[] Line2 = { new Vector2(v3.X, 0), v3 };
            fun.drawBLine(dxf, Line2, 0.3, contour);
            Vector2[] Line3 = { v3, v4 };
            fun.drawBLine(dxf, Line3, 0.3, contour);
            Vector2[] Line4 = { v6, v5 };
            fun.drawBLine(dxf, Line4, 0.3, contour);
            Vector2[] Line5 = { v6, new Vector2(v6.X, 0) };
            fun.drawBLine(dxf, Line5, 0.3, contour);
            LwPolyline Arc6 = new LwPolyline();
            Arc6.Vertexes.Add(new LwPolylineVertex(v5));
            Arc6.Vertexes.Add(new LwPolylineVertex(v4));
            Arc6.Vertexes[0].Bulge = 1;
            fun.PolyFill(dxf, Arc6, 0.3, contour);
            Vector2[] Line7 = { v7, v8 };
            fun.drawBLine(dxf, Line7, 0.3, contour);
            Vector2[] Line8 = { v8, v9 };
            fun.drawBLine(dxf, Line8, 0.3, contour);
            Vector2[] Line9 = { v9, v10 };
            fun.drawBLine(dxf, Line9, 0.3, contour);
            Vector2[] Line10 = { v10, v11 };
            fun.drawBLine(dxf, Line10, 0.3, contour);
            Vector2[] Line11 = { v11, v12 };
            fun.drawBLine(dxf, Line11, 0.3, contour);
            LwPolyline Arc12 = new LwPolyline();
            Arc12.Vertexes.Add(new LwPolylineVertex(v13));
            Arc12.Vertexes.Add(new LwPolylineVertex(v12));
            Arc12.Vertexes[0].Bulge = 1;
            fun.PolyFill(dxf, Arc12, 0.3, contour);
            Vector2[] Line13 = { v13, v14 };
            fun.drawBLine(dxf, Line13, 0.3, contour);
            Vector2[] Line14 = { v14, v15 };
            fun.drawBLine(dxf, Line14, 0.3, contour);
            Vector2[] Line15 = { v15, v16 };
            fun.drawBLine(dxf, Line15, 0.3, contour);
            Vector2[] Line16 = { v16, v1 };
            fun.drawBLine(dxf, Line16, 0.3, contour);
            Vector2[] Line17 = { v21, v22 };
            fun.drawBLine(dxf, Line17, 0.3, contour);
            /*LwPolyline Arc18 = new LwPolyline();
            Arc18.Vertexes.Add(new LwPolylineVertex(v18));
            Arc18.Vertexes.Add(new LwPolylineVertex(v17));
            Arc18.Vertexes[0].Bulge = 1;
            fun.PolyFill(dxf, Arc18, 0.3, contour);*/
            /*LwPolyline Arc19 = new LwPolyline();
            Arc19.Vertexes.Add(new LwPolylineVertex(v20));
            Arc19.Vertexes.Add(new LwPolylineVertex(v19));
            Arc19.Vertexes[0].Bulge = 1;
            fun.PolyFill(dxf, Arc19, 0.3, contour);*/
            Vector2[] Line20 = { v23, v24 };
            fun.drawPlinetype(dxf, Line20, 0.15, refin, Linetype.Dashed);
            Vector2[] Line21 = { v25, v26 };
            fun.drawPlinetype(dxf, Line21, 0.15, centre, Linetype.Center);
            Arc arc22 = new Arc(new Vector2(-oal / 2, -dc / 2), prfrad, 0, 180);
            Vector2[] Line22 = { add1, v4 };
            fun.drawBLine(dxf, Line22, 0.3, contour);
            Vector2[] Line23 = { add2, v5 };
            fun.drawBLine(dxf, Line23, 0.3, contour);

            //填充
            /*LwPolyline hatchpoly1 = new LwPolyline();
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v13));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v12));
            hatchpoly1.Vertexes[0].Bulge = 1;
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v19));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v20));
            hatchpoly1.Vertexes[2].Bulge = -1;
            hatchpoly1.IsClosed = true;
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = hatchpoly1.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)hatchpoly1.Explode()[i];
                }
                if (htype1[i] == "Arc")
                {
                    harc1[i] = (Arc)hatchpoly1.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, 45, 0.25, hline1, harc1, contour);*/

            /*LwPolyline hatchpoly2 = new LwPolyline();
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v18));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v17));
            hatchpoly2.Vertexes[0].Bulge = 1;
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v4));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v5));
            hatchpoly2.Vertexes[2].Bulge = -1;
            hatchpoly2.IsClosed = true;
            String[] htype2 = new String[100];
            Line[] hline2 = new Line[100];
            Arc[] harc2 = new Arc[100];
            int hnumst2 = hatchpoly2.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst2; i++)
            {
                htype2[i] = hatchpoly2.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype2[i] == "Line")
                {
                    hline2[i] = (Line)hatchpoly2.Explode()[i];
                }
                if (htype2[i] == "Arc")
                {
                    harc2[i] = (Arc)hatchpoly2.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype2, hnumst2, 45, 0.25, hline2, harc2, contour);*/

            LwPolyline hatchpoly3 = new LwPolyline();
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v11));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v12));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v13));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v14));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v15));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v16));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v21));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v22));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v9));
            hatchpoly3.Vertexes.Add(new LwPolylineVertex(v10));
            hatchpoly3.Vertexes[1].Bulge = -1;
            hatchpoly3.IsClosed = true;
            String[] htype3 = new String[100];
            Line[] hline3 = new Line[100];
            Arc[] harc3 = new Arc[100];
            int hnumst3 = hatchpoly3.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst3; i++)
            {
                htype3[i] = hatchpoly3.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype3[i] == "Line")
                {
                    hline3[i] = (Line)hatchpoly3.Explode()[i];
                }
                if (htype3[i] == "Arc")
                {
                    harc3[i] = (Arc)hatchpoly3.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype3, hnumst3, 45, 10, hline3, harc3, hatching);

            //标注样式
            //箭头样式
            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = 0.2;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;

            //文本样式
            dimensionStyle.TextHeight = 1.0;
            double offset = 10;

            double height = 2 * scale;
            double arrowsize = 1 * scale;
            double exline = 0.5 * scale;

            //坐标系
            /*fun.Axis(dxf, v25, 180, 270, "", "", "PCS", 0.3 * scale, height, 0);
            fun.Axis(dxf, fun.Polar(v10, -120 * pi / 180, 19.36 * scale), 180, 270, "Z", "Y", "X", 0.3 * scale, height, 0);*/

            //线段尺寸标注
            //尺寸1
            LinearDimension dim1 = new LinearDimension(new Line(v3, v6), offset, 0, dimensionStyle);
            dim1.UserText = "CW" /*+ Math.Round(cw / scale, 2)*/;
            dxf.AddEntity(dim1);
            //尺寸2
            LinearDimension dim2 = new LinearDimension(new Line(v6, v11), -offset * 2 - hhub, 270, dimensionStyle);
            dim2.UserText = "DC" /*+ Math.Round(dc / scale, 2)*/;
            dxf.AddEntity(dim2);
            //尺寸3
            if (dhub != 0)
            {
                LinearDimension dim3 = new LinearDimension(new Line(v9, v11), -offset - (v9.Y - v11.Y) / 2, 0, dimensionStyle);
                dim3.UserText = "HHUB" /*+ Math.Round(hhub / scale, 2)*/;
                dxf.AddEntity(dim3);
            }
            //尺寸5
            LinearDimension dim5 = new LinearDimension(new Line(v8, v1), offset * 2 + v3.Y - v1.Y, 0, dimensionStyle);
            dim5.UserText = "OAL" /*+ Math.Round(oal / scale, 2)*/;
            dxf.AddEntity(dim5);
            //尺寸6
            LinearDimension dim6 = new LinearDimension(new Line(v21, v24), offset, 270, dimensionStyle);
            dim6.UserText = "DCON" /*+ Math.Round(dcon / scale, 2)*/;
            dxf.AddEntity(dim6);
            //尺寸7
            if (dhub != 0)
            {
                LinearDimension dim7 = new LinearDimension(new Line(v9, v8), -offset, 270, dimensionStyle);
                dim7.UserText = "DHUB" /*+ Math.Round(dhub / scale, 2)*/;
                dxf.AddEntity(dim7);
            }
            //尺寸8
            RadialDimension dimrad8 = new RadialDimension(arc22, 45, -offset, dimensionStyle);//DimensionStyle.Default);
            dimrad8.UserText = "PRFRAD" /*+ Math.Round(prfrad / scale, 2)*/;
            dxf.AddEntity(dimrad8);

            //图框
            if (isframe == 1)
            {
                Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(-210 - oal / 2, 148.5));
                fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
            }

            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
